home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / Advanced AOCE Templates / Planet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-23  |  10.2 KB  |  428 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Planet.c
  3.  
  4.     Contains:    Planetary orbit display templates
  5.  
  6.     Written by:    Harry Chesley
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc.
  9.  
  10.     Change History (most recent first):
  11.  
  12.                    5/25/94    hrc        created
  13.  
  14.     To Do:
  15. */
  16.  
  17. #include <SANE.h>
  18. #include <String.h>
  19. #include <Packages.h>
  20. #include <Script.h>
  21. #include <OSUtils.h>
  22. #include <Math.h>
  23. #include <TextUtils.h>
  24. #include "OCETemplates.h"
  25. #include "Planet.h"
  26.  
  27. OSErr instanceInit(DETCallBlockPtr);
  28. OSErr convertToRString(DETCallBlockPtr);
  29. OSErr convertFromRString(DETCallBlockPtr);
  30. OSErr propertyDirtied(DETCallBlockPtr);
  31.  
  32. extended getExtendedProperty(DETCallBlockPtr, short);
  33. LongDateTime getTimeProperty(DETCallBlockPtr, short);
  34.  
  35. pascal OSErr Planet(DETCallBlockPtr callBlockPtr)
  36.     {
  37.     if (callBlockPtr->protoCall.target.selector == kDETSelf)
  38.         switch (callBlockPtr->protoCall.reqFunction)
  39.             {
  40.             case kDETcmdInstanceInit:        return instanceInit(callBlockPtr);
  41.             case kDETcmdConvertToRString:    return convertToRString(callBlockPtr);
  42.             case kDETcmdConvertFromRString:    return convertFromRString(callBlockPtr);
  43.             case kDETcmdPropertyDirtied:    return propertyDirtied(callBlockPtr);
  44.             }
  45.  
  46.     else if (callBlockPtr->protoCall.reqFunction == kDETcmdInit)
  47.         {
  48.         callBlockPtr->init.newCallFors = kDETCallForViewChanges;
  49.         return noErr;
  50.         }
  51.  
  52.     return kDETDidNotHandle;
  53.     }
  54.  
  55. OSErr instanceInit(DETCallBlockPtr callBlockPtr)
  56.     {
  57.     DETSetPropertyTypeBlock spt;
  58.     DETSetPropertyBinaryBlock spb;
  59.     unsigned long l;
  60.     LongDateCvt ldt;
  61.  
  62.     // Set the time property type
  63.     spt.reqFunction = kDETcmdSetPropertyType;
  64.     spt.target = callBlockPtr->protoCall.target;
  65.     spt.property = kTimeProperty;
  66.     spt.newType = kTimePropertyType;
  67.     CallBackDET(callBlockPtr, (DETCallBackBlock*) &spt);
  68.  
  69.     // Set the time property to the current time
  70.     GetDateTime(&l);
  71.     ldt.hl.lHigh = 0;
  72.     ldt.hl.lLow = l;
  73.     spb.reqFunction = kDETcmdSetPropertyBinary;
  74.     spb.target = callBlockPtr->protoCall.target;
  75.     spb.property = kTimeProperty;
  76.     spb.newValue = (Ptr) &ldt;
  77.     spb.newValueSize = sizeof(ldt);
  78.     if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb) == noErr)
  79.         {
  80.         // Dirty it
  81.         DETDirtyPropertyBlock dp;
  82.  
  83.         dp.reqFunction = kDETcmdDirtyProperty;
  84.         dp.target = callBlockPtr->protoCall.target;
  85.         dp.property = kTimeProperty;
  86.         CallBackDET(callBlockPtr, (DETCallBackBlock*) &dp);
  87.         }
  88.  
  89.     return noErr;
  90.     }
  91.  
  92. OSErr convertToRString(DETCallBlockPtr callBlockPtr)
  93.     {
  94.     DETConvertToRStringBlock* ctrs;
  95.     DETGetPropertyTypeBlock gpt;
  96.  
  97.     ctrs = &(callBlockPtr->convertToRString);
  98.  
  99.     // Get the type of the property being converted
  100.     gpt.reqFunction = kDETcmdGetPropertyType;
  101.     gpt.target = ctrs->target;
  102.     gpt.property = ctrs->property;
  103.     if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &gpt) == noErr)
  104.         {
  105.         char s[256];
  106.         RStringHandle h;
  107.  
  108.         // Convert time properties
  109.         if (gpt.propertyType == kTimePropertyType)
  110.             {
  111.             LongDateTime ldt;
  112.             char tStr[256];
  113.  
  114.             // Get the current value
  115.             ldt = getTimeProperty(callBlockPtr, ctrs->property);
  116.  
  117.             // Convert it to a string
  118.             iuldatestring(&ldt, shortDate, s, nil);
  119.             tStr[0] = ' ';
  120.             tStr[1] = 0;
  121.             strcat(s, tStr);
  122.             iultimestring(&ldt, true, tStr, nil);
  123.             strcat(s, tStr);
  124.             }
  125.  
  126.         // Convert floating point extended properties
  127.         else if (gpt.propertyType == kExtendedPropertyType)
  128.             {
  129.             extended n;
  130.             decform df;
  131.             decimal d;
  132.  
  133.             // Get the current value
  134.             n = getExtendedProperty(callBlockPtr, ctrs->property);
  135.  
  136.             // Convert it to a string
  137.             df.style = FLOATDECIMAL;
  138.             df.digits = 9;
  139.             num2dec(&df, n, &d);
  140.             dec2str(&df, &d, &s);
  141.             }
  142.  
  143.         // If we don't know the type, don't convert it
  144.         else return kDETDidNotHandle;
  145.  
  146.         // Return the string as an RString handle
  147.         h = (RStringHandle) NewHandle(strlen(s) + sizeof(ProtoRString));
  148.         if (h)
  149.             {
  150.             HLock((Handle) h);
  151.             OCECToRString(s, smRoman, *h, strlen(s));
  152.             HUnlock((Handle) h);
  153.             ctrs->theValue = h;
  154.  
  155.             return noErr;
  156.             }
  157.         else return MemError();
  158.         }
  159.  
  160.     return kDETDidNotHandle;
  161.     }
  162.  
  163. OSErr convertFromRString(DETCallBlockPtr callBlockPtr)
  164.     {
  165.     OSErr retVal;
  166.     DETConvertFromRStringBlock* cfrs;
  167.     DETGetPropertyTypeBlock gpt;
  168.  
  169.     retVal = kDETDidNotHandle;
  170.     cfrs = &(callBlockPtr->convertFromRString);
  171.  
  172.     // Get the property type to convert
  173.     gpt.reqFunction = kDETcmdGetPropertyType;
  174.     gpt.target = cfrs->target;
  175.     gpt.property = cfrs->property;
  176.     if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &gpt) == noErr)
  177.         {
  178.         char* str;
  179.  
  180.         // Convert the RString into a string
  181.         str = ((char*) &cfrs->theValue->dataLength) + 1;
  182.         p2cstr(str);
  183.  
  184.         // Convert time properties
  185.         if (gpt.propertyType == kTimePropertyType)
  186.             {
  187.             DateCacheRecord dc;
  188.  
  189.             // Init the date cache
  190.             if (InitDateCache(&dc) == noErr)
  191.                 {
  192.                 long lengthUsed;
  193.                 LongDateRec ldr;
  194.                 LongDateTime ldt;
  195.                 long strLength;
  196.                 DETSetPropertyBinaryBlock spb;
  197.  
  198.                 // Convert the string to a time
  199.                 strLength = strlen(str);
  200.                 String2Date(str, strLength, &dc, &lengthUsed, &ldr);
  201.                 if ((strLength - lengthUsed) <= 0)
  202.                     {
  203.                     ldr.ld.hour = 0;
  204.                     ldr.ld.minute = 0;
  205.                     ldr.ld.second = 0;
  206.                     }
  207.                 else String2Time(str + lengthUsed, strLength - lengthUsed, &dc, &lengthUsed, &ldr);
  208.                 LongDate2Secs(&ldr, &ldt);
  209.  
  210.                 // Set the property value
  211.                 spb.reqFunction = kDETcmdSetPropertyBinary;
  212.                 spb.target = cfrs->target;
  213.                 spb.property = cfrs->property;
  214.                 spb.newValue = (Ptr) &ldt;
  215.                 spb.newValueSize = sizeof(ldt);
  216.                 if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb) == noErr)
  217.                     {
  218.                     // Mark it as changed
  219.                     DETSetPropertyChangedBlock spc;
  220.                     spc.reqFunction = kDETcmdSetPropertyChanged;
  221.                     spc.target = cfrs->target;
  222.                     spc.property = cfrs->property;
  223.                     spc.propertyChanged = true;
  224.                     CallBackDET(callBlockPtr, (DETCallBackBlock*) &spc);
  225.                     }
  226.                 retVal = noErr;
  227.                 }
  228.             }
  229.  
  230.         // Convert floating point extended properties
  231.         else if (gpt.propertyType == kExtendedPropertyType)
  232.             {
  233.             DETSetPropertyBinaryBlock spb;
  234.             extended n;
  235.     
  236.             // Convert the string to an extended
  237.             n = 0;
  238.             if (cfrs->theValue->dataLength < 256)
  239.                 {
  240.                 short ix;
  241.                 decimal d;
  242.                 short vp;
  243.     
  244.                 ix = 0;
  245.                 str2dec(str, &ix, &d, &vp);
  246.                 if (vp) n = dec2num(&d);
  247.                 }
  248.  
  249.             // Set the value of the property
  250.             spb.reqFunction = kDETcmdSetPropertyBinary;
  251.             spb.target = cfrs->target;
  252.             spb.property = cfrs->property;
  253.             spb.newValue = (Ptr) &n;
  254.             spb.newValueSize = sizeof(n);
  255.             if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb) == noErr)
  256.                 {
  257.                 // Mark it as changed
  258.                 DETSetPropertyChangedBlock spc;
  259.                 spc.reqFunction = kDETcmdSetPropertyChanged;
  260.                 spc.target = cfrs->target;
  261.                 spc.property = cfrs->property;
  262.                 spc.propertyChanged = true;
  263.                 CallBackDET(callBlockPtr, (DETCallBackBlock*) &spc);
  264.                 }
  265.             retVal = noErr;
  266.             }
  267.  
  268.         // Return the original to it's normal state
  269.         c2pstr(str);
  270.         }
  271.  
  272.     return retVal;
  273.     }
  274.  
  275. // AU in meters
  276. #define kAU 149600000.0
  277.  
  278. // Sin with degrees
  279. extended degsin(extended d)
  280.     {
  281.     return sin(pi()*d/180.0);
  282.     }
  283.  
  284. // Cos with degrees
  285. extended degcos(extended d)
  286.     {
  287.     return cos(pi()*d/180.0);
  288.     }
  289.  
  290. // Returns days (including fractions) since 1990
  291. extended daysSince1990(LongDateTime t)
  292.     {
  293.     LongDateRec ldr;
  294.     LongDateTime t1990;
  295.     extended et, et1990;
  296.  
  297.     et = t;
  298.  
  299.     ldr.ld.era = 0;
  300.     ldr.ld.year = 1989;
  301.     ldr.ld.month = 12;
  302.     ldr.ld.day = 31;
  303.     ldr.ld.hour = 0;
  304.     ldr.ld.minute = 0;
  305.     ldr.ld.second = 0;
  306.     ldr.ld.pm = 0;
  307.     LongDate2Secs(&ldr, &t1990);
  308.     et1990 = t1990;
  309.  
  310.     return et / (24.0*60.0*60.0) - et1990 / (24.0*60.0*60.0);
  311.     }
  312.  
  313. OSErr propertyDirtied(DETCallBlockPtr callBlockPtr)
  314.     {
  315.     DETPropertyDirtiedBlock* pd;
  316.  
  317.     pd = (DETPropertyDirtiedBlock*) &callBlockPtr->propertyDirtied;
  318.     switch (pd->property)
  319.         {
  320.         // Only recalculate on selected properties
  321.         case kTimeProperty:
  322.         case kTpProperty:
  323.         case kEpsilonProperty:
  324.         case kOmegaBarProperty:
  325.         case keProperty:
  326.         case kaProperty:
  327.             {
  328.             DETSetPropertyTypeBlock spt;
  329.             DETSetPropertyBinaryBlock spb;
  330.             extended d, tp, epsilon, omegaBar, e, a;
  331.             extended n, m, l, v, r, x, y;
  332.  
  333.             // Get the input parameters
  334.             d = daysSince1990(getTimeProperty(callBlockPtr, kTimeProperty));
  335.             tp = getExtendedProperty(callBlockPtr, kTpProperty);
  336.             epsilon = getExtendedProperty(callBlockPtr, kEpsilonProperty);
  337.             omegaBar = getExtendedProperty(callBlockPtr, kOmegaBarProperty);
  338.             e = getExtendedProperty(callBlockPtr, keProperty);
  339.             a = getExtendedProperty(callBlockPtr, kaProperty);
  340.  
  341.             // If the parameters are zero, return zero
  342.             if (tp == 0.0)
  343.                 {
  344.                 x = 0.0;
  345.                 y = 0.0;
  346.                 }
  347.             // Otherwise, calculate the current position
  348.             else
  349.                 {
  350.                 n = fmod((360.0/365.242191)*(d/tp), 360.0);
  351.                 m = n+epsilon-omegaBar;
  352.                 l = fmod(n+(360.0/pi())*e*degsin(m)+epsilon, 360.0);
  353.                 v = l-omegaBar;
  354.                 r = kAU*(a*(1.0-e*e))/(1.0+e*degcos(v));
  355.                 x = degcos(l)*r;
  356.                 y = degsin(l)*r;
  357.                 }
  358.  
  359.             // Prepare to set the type and value of the x and y properties
  360.             spt.reqFunction = kDETcmdSetPropertyType;
  361.             spt.target = pd->target;
  362.  
  363.             spb.reqFunction = kDETcmdSetPropertyBinary;
  364.             spb.target = pd->target;
  365.  
  366.             // Set x's type
  367.             spt.property = kXProperty;
  368.             spt.newType = kExtendedPropertyType;
  369.             if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spt) == noErr)
  370.                 {
  371.                 // Set x's value
  372.                 spb.property = kXProperty;
  373.                 spb.newValue = (Ptr) &x;
  374.                 spb.newValueSize = sizeof(x);
  375.                 CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb);
  376.                 }
  377.  
  378.             // Set y's type
  379.             spt.property = kYProperty;
  380.             spt.newType = kExtendedPropertyType;
  381.             if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &spt) == noErr)
  382.                 {
  383.                 // Set y's value
  384.                 spb.property = kYProperty;
  385.                 spb.newValue = (Ptr) &y;
  386.                 spb.newValueSize = sizeof(y);
  387.                 CallBackDET(callBlockPtr, (DETCallBackBlock*) &spb);
  388.                 }
  389.  
  390.             return noErr;
  391.             }
  392.         }
  393.  
  394.     return kDETDidNotHandle;
  395.     }
  396.  
  397. extended getExtendedProperty(DETCallBlockPtr callBlockPtr, short property)
  398.     {
  399.     DETGetPropertyBinaryBlock gpb;
  400.     extended n;
  401.  
  402.     gpb.reqFunction = kDETcmdGetPropertyBinary;
  403.     gpb.target = callBlockPtr->protoCall.target;
  404.     gpb.property = property;
  405.     if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &gpb) != noErr) return 0.0;
  406.  
  407.     BlockMove(*gpb.propertyValue, (char*) &n, sizeof(n));
  408.     DisposHandle(gpb.propertyValue);
  409.  
  410.     return n;
  411.     }
  412.  
  413. LongDateTime getTimeProperty(DETCallBlockPtr callBlockPtr, short property)
  414.     {
  415.     DETGetPropertyBinaryBlock gpb;
  416.     LongDateTime ldt;
  417.  
  418.     gpb.reqFunction = kDETcmdGetPropertyBinary;
  419.     gpb.target = callBlockPtr->protoCall.target;
  420.     gpb.property = property;
  421.     if (CallBackDET(callBlockPtr, (DETCallBackBlock*) &gpb) != noErr) return 0;
  422.  
  423.     BlockMove(*gpb.propertyValue, (char*) &ldt, sizeof(ldt));
  424.     DisposHandle(gpb.propertyValue);
  425.  
  426.     return ldt;
  427.     }
  428.